package com.tao.lab.power.usb;


import android.util.Log;

import com.tao.lab.utils.ProfUseByteHexStringUtil;

import java.util.ArrayList;

/**
 * title: 超声波-中
 * description:
 * @author gongmin.li
 * @version 1.0.0
 * @since 2020-05-27
 */
public class ApiEyeLeftDevice implements UsbSerialIOListener<String> {

    private static final String TAG = ApiEyeLeftDevice.class.getSimpleName();

    // myvar.g_nOnce.val=0
    private final byte[] expBegin0 = new byte[]{
            (byte) 0x6D, (byte) 0x79, (byte) 0x76, (byte) 0x61,
            (byte) 0x72, (byte) 0x2E, (byte) 0x67, (byte) 0x5F, (byte) 0x6E, (byte) 0x4F,
            (byte) 0x6E, (byte) 0x63, (byte) 0x65, (byte) 0x2E, (byte) 0x76, (byte) 0x61,
            (byte) 0x6C, (byte) 0x3D, (byte) 0x30, (byte) 0xff, (byte) 0xff, (byte) 0xff};

    // myvar.g_nOnce.val=1
    private final byte[] expBegin1 = new byte[]{
            (byte) 0x6D, (byte) 0x79, (byte) 0x76, (byte) 0x61,
            (byte) 0x72, (byte) 0x2E, (byte) 0x67, (byte) 0x5F, (byte) 0x6E, (byte) 0x4F,
            (byte) 0x6E, (byte) 0x63, (byte) 0x65, (byte) 0x2E, (byte) 0x76, (byte) 0x61,
            (byte) 0x6C, (byte) 0x3D, (byte) 0x31, (byte) 0xff, (byte) 0xff, (byte) 0xff};

    // myvar.g_bRecvEnd.val=1
    //6D797661722E675F6252656376456E642E76616C3D31FFFFFF
    private final byte[] expEnd = new byte[]{
            (byte) 0x6D, (byte) 0x79, (byte) 0x76, (byte) 0x61, (byte) 0x72, (byte) 0x2E, (byte) 0x67,
            (byte) 0x5F, (byte) 0x62, (byte) 0x52, (byte) 0x65, (byte) 0x63, (byte) 0x76, (byte) 0x45, (byte) 0x6E,
            (byte) 0x64, (byte) 0x2E, (byte) 0x76, (byte) 0x61, (byte) 0x6C, (byte) 0x3D, (byte) 0x31, (byte) 0xff,
            (byte) 0xff, (byte) 0xff};

    //收到所有数据的十六进制字符串缓存，当解析到符合规则,copy走，并清空
    private StringBuilder dataCache;

    //解析到符合规则的数据缓存，取一次清一次，只保留最后一组(可能一条或多条)
    private volatile ArrayList<Float> returnList;

    private synchronized ArrayList<Float> getReturnList() {
        return returnList;
    }

    public ApiEyeLeftDevice() {
        dataCache = new StringBuilder();
        returnList = new ArrayList<>();
    }

    @Override
    public void onNewData(byte[] data) {
        String dataStr = ProfUseByteHexStringUtil.byte2HexString(data);
        //dataCache.append(dataStr);
        //
        ////$GPRMC,123450.00,A,3011.14761,N,12010.80122,E,0.095,,291220,,,A*7A
        ////244750524D432C3132333435302E30302C412C333031312E31343736312C4E2C31323031302E38303132322C452C302E3039352C2C3239313232302C2C2C412A37410D0A
        //int sofNormal = dataCache.indexOf("244750524D43");
        //int sufNormal = dataCache.indexOf("0D0A", sofNormal);
        //
        //String returnStr = null;
        //if (sofNormal != -1 && sufNormal != -1) {
        //    returnStr = dataCache.substring(sofNormal, sufNormal + 4);
        //    dataCache.delete(0, dataCache.length());
        //}
        //
        //if (null == returnStr || returnStr.length() < 40) {
        //    return;
        //}

    }

    @Override
    public void onRunError(Exception e) {
        Log.e(TAG, "onRunError: Serial Exception: ", e);

        dataCache.delete(0, dataCache.length());
        getReturnList().clear();
    }

    @Override
    public byte[] initDataFixedSend() {
        return expEnd;
    }

    @Override
    public void clearData() {
        dataCache.delete(0, dataCache.length());
        getReturnList().clear();
    }

}
